package javachongkao.面向对象下.继承覆盖综合练习;

import java.text.DecimalFormat;
import java.util.*;
//7-9 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company
//定义Person抽象类，Student类、Company类，Employee类。
//
//Person类的属性：String name, int age, boolean gender
//Person类的方法:
//
//public Person(String name, int age, boolean gender);
//public String toString();         //返回"name-age-gender"格式的字符串
//public boolean equals(Object obj);//比较name、age、gender,都相同返回true,否则返回false
//
//Student类继承自Person，属性:String stuNo, String clazz
//Student类的方法:
//
/// /建议使用super复用Person类的相关有参构造函数
//public Student(String name, int age, boolean gender, String stuNo, String clazz);
//public String toString();         //返回 “Student:person的toString-stuNo-clazz”格式的字符串
//public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true，则继续比较stuNo与clazz。
//Company类属性：String name
//Company类方法:
//
//public Company(String name);
//public String toString();         //直接返回name
//public boolean equals(Object obj);//name相同返回true
//Employee类继承自Person，属性：Company company, double salary
//Employee类方法：
//
////建议使用super复用Person类的相关有参构造函数
//public Employee(String name, int age, boolean gender, double salary, Company company);
//public String toString();         //返回"Employee:person的toString-company-salary"格式的字符串
//public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true。再比较company与salary。

////比较salary属性时，使用DecimalFormat df = new DecimalFormat("#.#");保留1位小数
//编写equals方法重要说明：
//
//对Employee的company属性的比较。要考虑传入为null的情况。如果company不为null且传入为null，返回false
//对所有String字符类型比较时，也要考虑null情况。
//提示
//
//排序可使用Collections.sort
//equals方法要考虑周全
//main方法说明
//创建若干Student对象、Employee对象。
//输入s，然后依次输入name age gender stuNo clazz创建Student对象。
//输入e，然后依次输入name age gender salary company创建Employee对象。
//然后将创建好的对象放入List<Person> personList。输入其他字符，则结束创建。
//创建说明: 对于String类型，如果为null则不创建对象，而赋值为null。对于company属性，如果为null则赋值为null，否则创建相应的Company对象。
//
//对personList中的元素实现先按照姓名升序排序，姓名相同再按照年龄升序排序。提示：可使用Comparable<Person>或Comparator<Person>
//
//接受输入，如果输入为exit则return退出程序，否则继续下面步骤。
//
//将personList中的元素按照类型分别放到stuList与empList。注意：不要将两个内容相同的对象放入列表（是否相同是根据equals返回结果进行判定）。
//
//输出字符串stuList，然后输出stuList中的每个对象。
//
//输出字符串empList，然后输出empList中的每个对象。
//
//1-3为一个测试点
//4-6为一个测试点
abstract class Person implements Comparable<Person> {
    protected String name;
    protected int age;
    protected boolean gender;

    public Person(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return name + "-" + age + "-" + gender;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person person = (Person) obj;
        return age == person.age &&
                gender == person.gender &&
                Objects.equals(name, person.name);
    }

    @Override
    public int compareTo(Person o) {
        int nameCompare = this.name.compareTo(o.name);
        if (nameCompare != 0) {
            return nameCompare;
        }
        return Integer.compare(this.age, o.age);
    }
}

class Student extends Person {
    private String stuNo;
    private String clazz;

    public Student(String name, int age, boolean gender, String stuNo, String clazz) {
        super(name, age, gender);
        this.stuNo = stuNo;
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "Student:" + super.toString() + "-" + stuNo + "-" + clazz;
    }

    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj)) return false;
        Student student = (Student) obj;
        return Objects.equals(stuNo, student.stuNo) &&
                Objects.equals(clazz, student.clazz);
    }
}

class Company {
    private String name;

    public Company(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Company company = (Company) obj;
        return Objects.equals(name, company.name);
    }
}

class Employee extends Person {
    private Company company;
    private double salary;

    public Employee(String name, int age, boolean gender, double salary, Company company) {
        super(name, age, gender);
        this.salary = salary;
        this.company = company;
    }

    @Override
    public String toString() {
        return "Employee:" + super.toString() + "-" +
                (company != null ? company.toString() : "null") + "-" + salary;
    }

    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj)) return false;
        Employee employee = (Employee) obj;

        DecimalFormat df = new DecimalFormat("#.#");
        String thisSalary = df.format(this.salary);
        String otherSalary = df.format(employee.salary);

        return Objects.equals(thisSalary, otherSalary) &&
                Objects.equals(company, employee.company);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Person> personList = new ArrayList<>();

        // 读取输入并创建对象
        while (scanner.hasNext()) {
            String type = scanner.next();
            if (!type.equals("s") && !type.equals("e")) {
                break;
            }

            String name = scanner.next();
            int age = scanner.nextInt();
            boolean gender = scanner.nextBoolean();

            if (type.equals("s")) {
                String stuNo = scanner.next();
                String clazz = scanner.next();
                if (clazz.equals("null")) clazz = null;
                personList.add(new Student(name, age, gender, stuNo, clazz));
            } else {
                double salary = scanner.nextDouble();
                String companyName = scanner.next();
                Company company = companyName.equals("null") ? null : new Company(companyName);
                personList.add(new Employee(name, age, gender, salary, company));
            }
        }

        // 排序
        Collections.sort(personList);

        // 输出排序后的personList
        for (Person p : personList) {
            System.out.println(p);
        }

        // 处理continue/exit
        if (scanner.hasNext()) {
            String command = scanner.next();
            if (command.equals("exit")) {
                scanner.close();
                return;
            }
        }

        // 分离到stuList和empList，去重
        List<Student> stuList = new ArrayList<>();
        List<Employee> empList = new ArrayList<>();

        for (Person p : personList) {
            if (p instanceof Student) {
                Student s = (Student) p;
                if (!stuList.contains(s)) {
                    stuList.add(s);
                }
            } else if (p instanceof Employee) {
                Employee e = (Employee) p;
                if (!empList.contains(e)) {
                    empList.add(e);
                }
            }
        }

        // 输出结果
        System.out.println("stuList");
        for (Student s : stuList) {
            System.out.println(s);
        }

        System.out.println("empList");
        for (Employee e : empList) {
            System.out.println(e);
        }

        scanner.close();
    }
}